package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.JSONStr;
import com.atguigu.gmall.feign.item.ItemFeignClient;
import com.atguigu.gmall.feign.product.ProductFeignClient;
import com.atguigu.gmall.model.to.SkuDetailTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

/**
 * @Author 卡尼奶
 * @Date 2022/5/22 16:01
 * @Version 1.0
 */
@Controller
@Slf4j
public class ItemController {

    @Autowired
    private ItemFeignClient itemFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;


    @GetMapping("/{skuId}.html")
    public String getProductDetail(@PathVariable("skuId")Long skuId, Model model){
        Result<SkuDetailTo> skuDetail = itemFeignClient.getSkuDetail(skuId);
        if (skuDetail.isOk()){
            SkuDetailTo data = skuDetail.getData();
            //1、当前sku所在的分类和完整信息；
            model.addAttribute("categoryView",data.getCategoryView());

            //2、当前sku基本信息
            model.addAttribute("skuInfo",data.getSkuInfo());



            //4、spuSaleAttrList
            model.addAttribute("spuSaleAttrList",data.getSpuSaleAttrList());

            //5、valuesSkuJson
            model.addAttribute("valuesSkuJson", JSONStr.toJsonStr(data.getValueSkuJson()));

        }
        Result<BigDecimal> price = productFeignClient.getPrice(skuId);
        //3、当前sku价格
        if (price.isOk()){
            model.addAttribute("price",price.getData());
        }
        return "item/index";
    }
}
